package entity.beans;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.*;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@NamedQueries({
	@NamedQuery (name="findUser", 
			query="SELECT u FROM User AS u WHERE u.username=?1", 
			hints={@QueryHint(name="org.hibernate.cacheable",value="true")
			,
			@QueryHint(name="org.hibernate.cacheRegion" ,value="findUser")}),
			
	@NamedQuery (name="authUser", 
			query="SELECT COUNT(*) FROM User AS u WHERE u.username=?1 AND u.password=?2", 
			hints={@QueryHint(name="org.hibernate.cacheable",value="true")
			,
			@QueryHint(name="org.hibernate.cacheRegion" ,value="authUser")}),		
})

@Entity
@Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL, region="User")
@Table(name="USERS")
public class User implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String username;
	private String password;
	private String name;
	private String lastname;
	private String address;
	private String cap;
	private String city;
	private String prov;
	private String tel;
	private Date subscriptionDate;
	private Date lastSession;

	public User (){};

	public User (String username, String password, String name, String lastname, String address, String cap, String city, String prov, Date subscriptionDate) {
		this.username=username.toLowerCase();
		this.password = password;
		this.name=name.toLowerCase();
		this.lastname=lastname.toLowerCase();
		this.address=address.toLowerCase();
		this.cap=cap;
		this.city=city.toLowerCase();
		this.prov=prov;
		this.subscriptionDate=subscriptionDate;
	}

	@Id
	@Column(name="username", unique=true, nullable=false, length=20)
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username.toLowerCase();
	}

	@Column(name="password", nullable=false, length=10)
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	@Column(name="name", nullable=false, length=60)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name.toLowerCase();
	}

	@Column(name="lastname", nullable=false, length=60)
	public String getLastname() {
		return lastname;
	}
	public void setLastname(String lastname) {
		this.lastname = lastname.toLowerCase();
	}

	@Column(name="address", nullable=false, length=60)
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address.toLowerCase();
	}

	@Column(name="cap", nullable=false, length=5)
	public String getCap() {
		return cap;
	}
	public void setCap(String cap) {
		this.cap = cap;
	}

	@Column(name="city", nullable=false, length=60)
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city.toLowerCase();
	}

	@Column(name="prov", nullable=false, length=2)
	public String getProv() {
		return prov;
	}
	public void setProv(String prov) {
		this.prov = prov;
	}

	@Column(name="tel", length=10)
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}

	@Column(name="subscription")
	@Temporal(TemporalType.TIMESTAMP)
	public Date getSubscriptionDate() {
		return subscriptionDate;
	}
	public void setSubscriptionDate(Date subscriptionDate) {
		this.subscriptionDate = subscriptionDate;
	}

	@Column(name="session", updatable = true)
	@Temporal(TemporalType.TIMESTAMP)
	public Date getLastSession() {
		return lastSession;
	}
	public void setLastSession(Date lastSession) {
		this.lastSession = lastSession;
	}	
}